package com.tyros.web.log;

import com.tyros.web.log.aspect.ControllerAnnotationAspect;
import com.tyros.web.log.aspect.ControllerPackageAspect;
import com.tyros.web.log.aspect.SwaggerAnnotationAspect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author zhangshuaiyin
 * @date 2022/7/2 10:57
 */
@Slf4j
@Configuration
@ConditionalOnClass(WebMvcConfigurer.class)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@EnableConfigurationProperties(HandlerLogProperties.class)
@ConditionalOnProperty(prefix = "tyros.web.handler-log", name = "enable", havingValue = "true", matchIfMissing = true)
public class HandlerLogAutoConfiguration {
    private final HandlerLogProperties properties;

    public HandlerLogAutoConfiguration(HandlerLogProperties handlerLogProperties) {
        this.properties = handlerLogProperties;
    }

    @Bean
    @ConditionalOnProperty(prefix = "tyros.web.handler-log", name = "aspectType", havingValue = "swaggerAnnotation")
    public SwaggerAnnotationAspect annotationAspect() {
        return new SwaggerAnnotationAspect(properties);
    }

    @Bean
    @ConditionalOnProperty(prefix = "tyros.web.handler-log", name = "aspectType", havingValue = "controllerAnnotation")
    public ControllerAnnotationAspect controllerAnnotation() {
        return new ControllerAnnotationAspect(properties);
    }

    @Bean
    @ConditionalOnProperty(prefix = "tyros.web.handler-log", name = "aspectType", havingValue = "controllerPackage")
    public ControllerPackageAspect controllerPackage() {
        return new ControllerPackageAspect(properties);
    }
}
